#!/usr/bin/bash

NAME=wifi
SIS=sis-$NAME
HELP=help-$NAME
logmsg="/home/gnu/sh/sis-logmsg"


# conf:
# auto_connect='false'
auto_connect='true'



key=$1
param=$2

if [[ "" == $key ]] ; then
	cat ~/.sway/help/$SIS
	
	exit 1
elif [[ "--" == $key ]] ; then
	~/bin/sis/$HELP $param
	exit 2
else
	name=$2
fi



if [[ up == $key ]] ; then
	$logmsg 'wifi up starting..'

	$logmsg 'wifi adapter up'
	iwctl adapter phy0 set-property Powered on

	$logmsg 'wifi device up'
	iwctl device wlan0 set-property Powered on

	# iwctl wsc wlan0 start-pin

	$logmsg 'wifi start list updating'	
	iwctl station wlan0 scan
	
	if [[ 'true' == $auto_connect ]] ; then
		$SIS connect
	fi
	
	$logmsg 'wifi up'
elif [[ down == $key ]] ; then
	$logmsg "wifi down starting..."

	$logmsg 'wifi connect down'
	iwctl station wlan0 disconnect 

	$logmsg 'wifi device down'
	iwctl device wlan0 set-property Powered off

	$logmsg 'wifi adapter down'
	iwctl adapter phy0 set-property Powered off

	$logmsg 'wifi down'
elif [[ connect == $key ]] ; then
	$logmsg "wifi connect starting..."

	# wifi to on: - commit this, if you not want wifi to on:
	# sis-wifi up

	iwctl station wlan0 get-networks

	# check and write name wifi
	name=$2
	if [[ "" == $name ]] ; then
		# default
		name=`cat ~/wifi_name`
	fi

	iwctl station wlan0 connect $name
	
	$logmsg 'wifi connect'
elif [[ get == $key ]] ; then
	# new get with more speed:
	ip addr | grep wlan | grep UP > /dev/null 2> /dev/null

	if [[ 1 == $? ]] ; then
		$logmsg -t SECURITY 'level your security to up - becose wifi down :D'
		$logmsg 'wifi is down'
		echo down
	else
		$logmsg -t SECURITY 'level your security to down - becose wifi up :C'
		$logmsg 'wifi is up'
		echo up
	fi 
elif [[ "toggle" == $key ]] ; then
	if [[ `$SIS get` == up ]] ; then
		$SIS down
	else
		$SIS up
	fi
elif [[ "save" == $key ]] ; then
	echo `sis-wifi get` > ~/.config/sis/save/wifi.save
	# echo `sis-wifi get mute` > ~/.config/sis/save/wifi.mute # save mute
elif [[ "load" == $key ]] ; then
	$SIS `cat ~/.config/sis/save/wifi.save`
	# $volume `cat ~/.config/sis/save/wifi.mute`
fi

exit 0
